home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18489 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  54 lines

  1. Newsgroups: comp.lang.c++
  2. Path: link-1.ts.bcc.ac.uk!ucecb09
  3. From: ucecb09@ucl.ac.uk (Robert Byrne)
  4. Subject: Re: C++ complex numbers slow as molasses?
  5. Message-ID: <1996Apr18.094043.62430@ucl.ac.uk>
  6. Date: Thu, 18 Apr 1996 09:40:43 GMT
  7. References: <4k6k8c$8h4@nyx.cs.du.edu> <9604071851.AA001o1@lorelei.demon.co.uk> <4ke5ap$b8b@mag1.magmacom.com> <Pine.LNX.3.91.960412073943.308E-100000@quoin1.quoininc.com> <4ktki7$vft@mag1.magmacom.com>
  8. Organization: University College London
  9.  
  10. ezust@mag1.magmacom.com (Acme Instant Dehydrated Boulder Kit) writes:
  11. >Jean Pierre LeJacq  <jplejacq@quoininc.com> wrote:
  12. >>
  13. >>class complex {
  14. >>   public:
  15. >>      complex & operator +=(complex const & lhs);
  16. >>};
  17.  
  18. >The choices are:
  19. >If you use the +=, you must first initialize the destination explicitly, and
  20. >then call it twice to achieve the same functionality as one call to the
  21. >binary operator+ 
  22.  
  23. Two calls?
  24.     complex res = lhs;
  25.     res += rhs;
  26. I can't see why two calls would be necessary unless you envision
  27.     complex res = 0;
  28.     res += lhs;
  29.     res += rhs;
  30. for which I see no obvious advantage.
  31.  
  32. >But if one is really concerned about runtime efficiency, chances are that a
  33. >2-operand member function add can be written so it is more efficient than
  34. >the operator+= used in the way described above.
  35.  
  36. Can someone demonstrate this? Personally I can't see it happening for a
  37. class like complex even if the compiler elides the cp ctor for code
  38. such as
  39.  
  40.     complex operator+(const complex &lhs, const complex &rhs)
  41.     {
  42.         return complex(lhs) += rhs;
  43.     }
  44.  
  45. The binary operator+ is still not going to be as efficient as the
  46. assignment version. Although I would be pleased to see this disproved.
  47.  
  48. Rob
  49.  
  50. --
  51. Robert Byrne         `I think therefore I am' says rather more than
  52. ucecb09@ucl.ac.uk     is strictly certain.'       -Bertrand Russell
  53.  
  54.